package pingce.share.model;

import java.io.Serializable;
import java.util.HashMap;

/**
 * 存放一个子句的分词结果
 * @author ucai
 *
 */
public class SplitResult implements Serializable{
	private static final long serialVersionUID = -9114501874335164314L;
	private String noPosResult;
	private String[] noPosArrayResult;
	private HashMap<String, PairValue> resultMap;
	public SplitResult() {
		super();
		this.resultMap = new HashMap<String, PairValue>();
	}
	public SplitResult(String resultWithPOS, int subSentenceBegin){
		this();
		String[] strArr = resultWithPOS.split(" ");
		if(resultWithPOS.equals(" ")){
			strArr = new String[]{" "};
		}
		this.noPosArrayResult = new String[strArr.length];
		StringBuilder sb = new StringBuilder();
		int offset = subSentenceBegin;
		for(int i = 0; i < strArr.length; i++){
			if(strArr[i].length() < 1)
				strArr[i] = " ";
			String token = filterPOS(strArr[i]);
			String pos = getPOS(strArr[i]);
			noPosArrayResult[i] = token;
			if(i != 0)
				sb.append(" ");
			sb.append(token);
			PairValue pv = new PairValue(token, pos);
			String key = offset + ":" + (offset + token.length() - 1);
			this.resultMap.put(key, pv);
			offset += token.length();
		}
		this.noPosResult = sb.toString();
	}
	public String getNoPosResult() {
		return noPosResult;
	}
	public void setNoPosResult(String noPosResult) {
		this.noPosResult = noPosResult;
	}
	public String[] getNoPosArrayResult() {
		return noPosArrayResult;
	}
	public void setNoPosArrayResult(String[] noPosArrayResult) {
		this.noPosArrayResult = noPosArrayResult;
	}
	public HashMap<String, PairValue> getResultMap() {
		return resultMap;
	}
	public void setResultMap(HashMap<String, PairValue> resultMap) {
		this.resultMap = resultMap;
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("\nsplit result =" + this.getNoPosResult() + "\n");
		sb.append("split result(array) =");
		for(String s : this.noPosArrayResult)
			sb.append(s + " ");
		sb.append("\ntoken:pos:position:\n");
		for(String position : this.resultMap.keySet()){
			PairValue pv = this.resultMap.get(position);
			sb.append("\t" + pv.getBasic() + ":" + pv.getAttach() + ":" + position + "\n");
		}
		
		return sb.toString();
	}
	
	public static String filterPOS(String token){
		int position = token.lastIndexOf("/");
		if( position != -1)
			token = token.substring(0, position);
		return token;
	}
	
	public static String getPOS(String token){
		String pos = "";
		int position = token.lastIndexOf("/");
		if(position != -1)
			pos = token.substring(position, token.length());
		return pos;
	}
}
